package pkg.tresEnRaya;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

/**
 *
 * @author usuario
 */
public class Interface extends JFrame implements ItemListener, ActionListener{
	public JButton b1;
	public JButton b2;
	public JButton b3;
	public JButton b4;
	public JButton b5;
	public JButton b6;
	public JButton b7;
	public JButton b8;
	public JButton b9;
    private Tablero tablero=null;
    public JButton reset=null;
    private JLabel msg=null;
    public JRadioButton c1=null;
    public JRadioButton c2=null;
    
    public Interface()
    {
        this.setTitle("Tres en Raya");
        this.setBackground(Color.WHITE);
        this.getContentPane().setLayout(new BorderLayout());
        this.addWindowListener(new Acabar());
        
        this.msg=new JLabel();
        this.tablero=new Tablero(this.msg);
        this.getContentPane().add(this.tablero, BorderLayout.CENTER);
        
        JPanel aux1= new JPanel();
        aux1.setLayout(new GridLayout(3,1));
        JPanel aux2= new JPanel();
        aux2.setLayout(new GridLayout(2,1));
        ButtonGroup cg= new ButtonGroup();
        c1=new JRadioButton("Humano vs Ordenador", true);
        cg.add(c1);
        c1.addItemListener(this);
        c2=new JRadioButton("Humano vs Humano");
        cg.add(c2);
        c2.addItemListener(this);
        
        aux2.add(c1);
        aux2.add(c2);
        aux1.add(aux2);
        aux1.add(msg);
        
        aux2= new JPanel();
        this.reset= new JButton("Reiniciar");
        this.reset.addActionListener(this);
        aux2.add(this.reset);
        aux1.add(aux2);
        
        this.add(aux1, BorderLayout.EAST);
    }

    
    @Override
    public void itemStateChanged(ItemEvent ie) {
    msg.setText("");
    tablero.empezarDeNuevo();
    if(ie.getSource()==c1)
    {
        tablero.setOrdenador(true);
    }
    else
    {
        tablero.setOrdenador(false);
    }
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
    msg.setText("");
    tablero.empezarDeNuevo();
    }

    public class Acabar extends WindowAdapter {

        @Override
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    }
}

